package com.ruoyi.personnel.uploadUtils;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileLoadedtoLocalhost {

    public static Boolean fileLoadedtoLocalhost(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            System.out.println("文件名: " + fileName);
            byte[] bytes = file.getBytes();

            // 构建文件的完整保存路径
            Path path = Paths.get("D:\\ruoyi\\uploadPath\\upload\\" + fileName);

            // 确保路径的父目录存在，如果不存在则创建
            Files.createDirectories(path.getParent());

            // 打开文件以写入，如果文件存在则覆盖
            try (java.io.FileOutputStream fos = new java.io.FileOutputStream(path.toFile(), true)) {
                fos.write(bytes);
            }

            return true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}